import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))

import requests
import json
from reqapi.api import BaseApi
from com.db.order_account_manager import OrderAccountManager

class GetCsgoPagedListV2Api(BaseApi):
    def execute(self, filter_map: dict, game_id: int, list_sort_type: int, 
                list_type: int, min_price: str, max_price: str, page_index: int, 
                page_size: int, property_filter_tags: list, sort_type: int, 
                sticker_abrade: int, stickers_is_sort: bool, session_id: str):
        """获取CSGO商品分页列表V2
        
        Args:
            filter_map: 过滤条件映射
            game_id: 游戏ID
            list_sort_type: 列表排序类型
            list_type: 列表类型
            min_price: 最低价格
            max_price: 最高价格
            page_index: 页码
            page_size: 每页数量
            property_filter_tags: 属性过滤标签
            sort_type: 排序类型
            sticker_abrade: 贴纸磨损
            stickers_is_sort: 贴纸是否排序
            session_id: 会话ID
        """
        url = f"{self.BASE_URL}/api/homepage/new/es/template/GetCsGoPagedListV2"
        
        data = {
            "filterMap": filter_map,
            "gameId": game_id,
            "listSortType": list_sort_type,
            "listType": list_type,
            "maxPrice": max_price,
            "minPrice": min_price,
            "pageCode": "MARKET_PAGE_BUY_COMMODITY_PAGE",
            "pageIndex": page_index,
            "pageSize": page_size,
            "propertyFilterTags": property_filter_tags,
            "sortType": sort_type,
            "stickerAbrade": sticker_abrade,
            "stickersIsSort": stickers_is_sort,
            "sessionId": self.device_id
        }
        
        headers = self.common_headers
        
        print("请求头:")
        print(json.dumps(headers, indent=2, ensure_ascii=False))
        print("\n请求参数:")
        print(json.dumps(data, indent=2, ensure_ascii=False))
        
        try:
            response = self.interceptor.send_request(url, json=data, headers=headers, need_encrypt=False)
            return response
        except Exception as e:
            print(f"请求失败: {e}")
            return None

def main():
    # 从下单账号管理器获取ID为1的账号
    account_manager = OrderAccountManager()
    account = account_manager.get_account_by_id(1)
    
    if not account:
        print("无法获取ID为1的账号")
        return
    
    device_id = account.get('device_id', '')
    auth_token = account.get('uu_token', '')
    uk = account.get('uk', '')
    
    if not device_id or not auth_token or not uk:
        print("账号信息不完整，缺少必要的认证信息")
        return
    

    
    filter_map = {
        "Exterior": ["WearCategory1", "WearCategory2"],
        "Type": [
            "CSGO_Type_Pistol_Unlimited",
            "CSGO_Type_Rifle_Unlimited",
            "CSGO_Type_SMG_Unlimited",
            "CSGO_Type_Shotgun_Unlimited",
            "CSGO_Type_Machinegun_Unlimited"
        ]
    }
    
    api = GetCsgoPagedListV2Api(device_id, device_id, auth_token, uk)
    result = api.execute(
        filter_map=filter_map,
        game_id=730,
        list_sort_type=0,
        list_type=10,
        min_price="5",
        max_price="100",
        page_index=1,
        page_size=100,
        property_filter_tags=[],
        sort_type=0,
        sticker_abrade=0,
        stickers_is_sort=False,
        session_id=device_id
    )

if __name__ == "__main__":
    main()